package com.zhiyiang.service.hotel;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhiyiang.mapper.HotelMapper;
import com.zhiyiang.pojo.Hotel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class HotelServiceImpl implements HotelService{
    @Autowired
    private HotelMapper hotelMapper;

    @Override
    public List<Hotel> findAll() {
        List<Hotel> hotels = hotelMapper.selectList(null);
        return hotels;
    }

    @Override
    public Page<Hotel> getAllHotel(Integer current, Integer size) {
        Page<Hotel> page = new Page<>(current, size);
        Page<Hotel> hotelPage = hotelMapper.selectPage(page, null);
        return hotelPage;
    }

    @Override
    public Page<Hotel> getHotel(String query, Integer current, Integer size) {
        Page<Hotel> page = new Page<>(1, size);
        QueryWrapper<Hotel> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasLength(query),"hotelname", query);
        Page<Hotel> hotelPage = hotelMapper.selectPage(page, queryWrapper);
        return hotelPage;
    }

    @Override
    public Hotel getHotel(String hotelcode) {
        QueryWrapper<Hotel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.hasLength(hotelcode),"hotelcode", hotelcode);
        Hotel hotel = hotelMapper.selectOne(queryWrapper);
        return hotel;
    }

    @Override
    public Boolean addHotel(Hotel hotel) {
        Boolean flag = false;
        int insert = hotelMapper.insert(hotel);
        if (insert>0){
            flag = true;
        }
        return flag;
    }

    @Override
    public Boolean updateHotel(String hotelcode, Hotel hotel) {
        QueryWrapper<Hotel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.hasLength(hotelcode),"hotelcode", hotelcode);
        int update = hotelMapper.update(hotel,queryWrapper);
        boolean flag = false;
        if (update>0){
            flag=true;
        }
        return flag;
    }

    @Override
    public Boolean deleteHotel(String hotelcode) {
        QueryWrapper<Hotel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.hasLength(hotelcode), "hotelcode", hotelcode);
        int delete = hotelMapper.delete(queryWrapper);
        Boolean flag = false;
        if (delete>0){
            flag = true;
        }
        return flag;
    }

    @Override
    public List<Hotel> getHotels() {
        return hotelMapper.selectList(null);
    }
}
